// pages/buy/buy.js
var Protocol = require ("../../Controls/Protocol")
var DataExchange = require ("../../Controls/DataExchange")
const pay = require('../../utils/pay')

var app = getApp()

var getBtnStr = function(config)
{
  return `支付${config.price}并发布`;
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:
    {
      avatarUrl: "../../image/yellowchild.jpg",
      nickName: "熊二",
      money: 0,
    },
    btn_txt: "支付￥6并发布秘袋",
    items:[
      { id:1, times: "1", price: 6, value: 1, checked: true, bg:"../../image/buy/1.png",    mark:"../../image/buy/check1.png"},
      { id:2, times: "20", price: 100, value: 20, checked: false, bg: "../../image/buy/2.png",
        mark: "../../image/buy/check2.png" },
      { id:3, times: "100", price: 360, value: 100, checked: false, bg: "../../image/buy/3.png",
        mark: "../../image/buy/check3.png" },
    ],
    productData: null,
  },

  onShow:function()
  {
    this.setData({
      productData: DataExchange.get_publishData(),
      userInfo: app.globalData.userInfo,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const midaiwuConfig = DataExchange.get_midaiwuConfig();
    for(var i = 0; i < midaiwuConfig.length; i++)
    {
      for(var j = 0; j < this.data.items.length; j++)
      {
        if(this.data.items[j].id == midaiwuConfig[i].id)
        {
          this.data.items[j].times = midaiwuConfig[i].times;
          this.data.items[j].price = midaiwuConfig[i].price;
        }
      }
    }

    var currentSelectedConfig = null;
    for(var i = 0; i < this.data.items.length; i++)
    {
      if(this.data.items[i].checked)
      {
        currentSelectedConfig = this.data.items[i];
      }
    }

    this.setData({ items: this.data.items, btn_txt: getBtnStr(currentSelectedConfig) });
  },

  radioChange: function (e)
  {
    var items = this.data.items;
    var selectConfig = e.currentTarget.dataset.config;
    for (var i = 0; i < items.length; i++) {
      items[i].checked = false;
      if (items[i].times == selectConfig.times) {
        selectConfig.checked = true;
        items[i] = selectConfig;
      }
    }

    this.setData({ items: items, btn_txt: getBtnStr(selectConfig)});
  },

  //购买并发布
  btnBuyIvk:function()
  {
    console.log(DataExchange.get_publishData());
    //购买成功回调
    var publishIvk = function () {
      var duration = 0;
      for (var i = 0; i < this.data.productData.times.length; i++) {
        if (this.data.productData.times[i].checked) {
          duration = this.data.productData.times[i].value * 60;
          break;
        }
      }

      var publishData =
        {
          name: this.data.productData.name,
          maxPrice: this.data.productData.price,
          minPrice: this.data.productData.price,
          qty: this.data.productData.count,
          desc: this.data.productData.desc,
          duration: duration,
          images: this.data.productData.images,
        }

      Protocol.publish(publishData);
    }

    var buyResult = 
    {
      success:function(res)
      {
        console.log("购买成功");
        var app = getApp();
        app.globalData.credit.publishTimes += 10;
        wx.navigateTo({
          url: '../../pages/product/product?preview=true&delta=2',
        })
      },
      fail:function(res)
      {
        console.log("购买失败");
      },
      complete:function()
      {
        console.log("购买完成");
      }
    }

    var selecteditem = null;
    //购买
    for(var i = 0, len = this.data.items.length; i < len; i++)
    {
      if(this.data.items[i].checked)
      {
        selecteditem = this.data.items[i];
        break;
      }
    }

    if(!selecteditem)
    {
      return;
    }


    console.log("当前余额："+app.globalData.credit.balance);
    if (selecteditem.price <= app.globalData.credit.balance)
    {
      Protocol.subscribe(selecteditem.id, buyResult);
      return;
    }

    const order = 
    {
      amount: parseInt((selecteditem.price - app.globalData.credit.balance) * 100),
      body: '账号充值'
    };

    const that = this;
    pay(order, {
      success:()=>
      {
        that.btnBuyIvk();
      }, 
      fail:()=>
      {
        console.log('支付失败');
      }
    });
  }
})